//
//  MHCategoryMainViewController.m
//  ManHua
//
//  Created by 赵永泉 on 15/10/24.
//  Copyright © 2015年 蓝欧科技. All rights reserved.
//

#import "MHCategoryMainViewController.h"
#import "FDSlideBar.h"
#import "FDSlideContentView.h"
#import "MHCategoryDetailViewController.h"
#import "MHComicDetailViewController.h"
#import "MHCategory.h"
#import "MHCategoryCell.h"
#import "MHDataManager.h"
#import <AFNetworking.h>
#define kCategoryHeader @"http://112.124.96.190:9090/bukamanhua5appjobs/ComicHandle.ashx?method=catalogs"
@interface MHCategoryMainViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>
@property (strong, nonatomic) FDSlideBar *slideBar;
@property (strong, nonatomic) FDSlideContentView *contentView;
@property (nonatomic, strong) NSMutableArray *categoryArray;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation MHCategoryMainViewController

- (void)acquireData
{
    [[AFHTTPRequestOperationManager manager] GET:kCategoryHeader parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSArray *dataArray = responseObject;
        self.categoryArray = [NSMutableArray array];
        NSMutableArray *titleArray = [NSMutableArray array];
        [[MHDataManager shareManager]deleteValueFromeTable:@"CATEGORY"];
        for (NSDictionary *dict in dataArray) {
            MHCategory *category = [MHCategory modelWithDict:dict];
            [titleArray addObject:dict[@"CatalogName"]];
            [self.categoryArray addObject:category];
            [[MHDataManager shareManager]insertValueToTabel:@"CATEGORY" category:category];
        }
        self.slideBar.itemsTitle = titleArray;
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        NSLog(@"%@", error.localizedDescription);
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =[UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.title = @"分类";
    self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:kThemeColor,UITextAttributeTextColor,nil];
    self.categoryArray=  [[MHDataManager shareManager]findMainTopicModelData:@"CATEGORY"];
    
    FDSlideBar *sliderBar = [[FDSlideBar alloc] initWithFrame:CGRectMake(0, 64, Width, 30)];
    sliderBar.backgroundColor = [UIColor whiteColor];
    sliderBar.itemColor = [UIColor grayColor];
    sliderBar.itemSelectedColor = kThemeColor;
    sliderBar.sliderColor = kThemeColor;
    [sliderBar slideBarItemSelectedCallback:^(NSUInteger idx) {
        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:idx inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
    }];
    [self.view addSubview:sliderBar];
    self.slideBar = sliderBar;
    if (self.categoryArray.count!=0) {
        NSMutableArray *titleArray=[NSMutableArray arrayWithCapacity:0];
        for (MHCategory *categoryModel in self.categoryArray) {
            [titleArray addObject:categoryModel.CatalogName];
        }
        self.slideBar.itemsTitle = titleArray;
    }
    [self setupTableView];
    [self acquireData];
}

- (void)setupTableView {
    CGRect frame = CGRectMake(0, 0, CGRectGetMaxY(self.view.frame) - CGRectGetMaxY(self.slideBar.frame), CGRectGetWidth(self.view.frame));
    self.tableView = [[UITableView alloc] initWithFrame:frame];
    [self.view addSubview:self.tableView];
    
    [self.tableView registerClass:[MHCategoryCell class] forCellReuseIdentifier:@"CategoryCell"];
    
    self.tableView.center = CGPointMake(CGRectGetWidth(self.view.frame) * 0.5, CGRectGetHeight(self.view.frame) * 0.5 + CGRectGetMaxY(self.slideBar.frame) * 0.5 );
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.transform = CGAffineTransformMakeRotation(-M_PI_2);
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.pagingEnabled = YES;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.categoryArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MHCategoryCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"CategoryCell"];
    
    cell.contentView.transform = CGAffineTransformMakeRotation(M_PI_2);
    cell.CatalogID = [self.categoryArray[indexPath.row] CatalogID];
    __weak MHCategoryMainViewController *test = self;
    cell.pushComicDetailVCBlock = ^(MHComicIntroduct *comicIntroduct)
    {
        MHComicDetailViewController *comicDetailVC = [[MHComicDetailViewController alloc]init];
        comicDetailVC.comicIntroduct = comicIntroduct;
        [test.navigationController pushViewController:comicDetailVC animated:YES];
    };

    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return Width;
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:scrollView.contentOffset];
    
    [self.slideBar selectSlideBarItemAtIndex:indexPath.row];
}

@end
